namespace ManoMachine.Emulator.CPU
{
    #region

    using System;
    using System.Collections.Generic;

    #endregion

    public class Cpu : ICloneable
    {
        #region Constants and Fields

        private static Cpu cpu = new Cpu();

        #endregion

        #region Constructors and Destructors

        private Cpu()
        {
            Array registers = typeof(Registers).GetEnumValues();
            this.ComputerRegisters = new Dictionary<Registers, short>();
            foreach (var register in registers)
            {
                this.ComputerRegisters.Add((Registers)register, 0);
            }

            Array flags = typeof(Flags).GetEnumValues();
            this.ComputerFlags = new Dictionary<Flags, bool>();

            foreach (var flag in flags)
            {
                this.ComputerFlags.Add((Flags)flag, false);
            }
        }

        #endregion

        #region Properties

        /// <summary>
        ///   Get current cpu instance
        /// </summary>
        public static Cpu Current
        {
            get
            {
                return cpu;
            }
        }

        public string TName { get; set; }

        public Dictionary<Flags, bool> ComputerFlags { get; private set; }

        public Dictionary<Registers, short> ComputerRegisters { get; private set; }

        #endregion

        #region Implemented Interfaces

        #region ICloneable

        public object Clone()
        {
            Cpu cpuImage = new Cpu();

            foreach (var computerRegister in this.ComputerRegisters)
            {
                // cpuImage.ComputerRegisters = new Dictionary<Registers, short>();
                cpuImage.ComputerRegisters[computerRegister.Key] = computerRegister.Value;
            }

            foreach (var computerFlag in this.ComputerFlags)
            {
                // cpuImage.ComputerFlags = new Dictionary<Flags, bool>();
                cpuImage.ComputerFlags[computerFlag.Key] = computerFlag.Value;
            }

            return cpuImage;
        }

        #endregion

        #endregion
    }
}